using System;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace GameEngine.Input
{
    /// <summary>
    /// The method to excecute onupdate
    /// </summary>
    public delegate void UpdateMethod();
    public class ButtonList : Collection<ButtonState> { }
    public class KeyList : Collection<Keys> { }
    public struct MouseLocation { public int X; public int Y; }

    /// <summary>
    /// Gets input
    /// </summary>
    public class GEInput : GameComponent
    {
        private ButtonList buttonList;
        private KeyList keyList;
        private MouseLocation currentLocation;
        private MouseLocation lastLocation;

        /// <summary>
        /// Mouse buttons pressed
        /// </summary>
        public ButtonList PressedButtons { get { return buttonList; } }
        /// <summary>
        /// Keyboard buttons pressed
        /// </summary>
        public KeyList PressedKeys { get { return keyList; } }
        /// <summary>
        /// The mouse's current location
        /// </summary>
        public MouseLocation CurrentMouseLocation { get { return currentLocation; } }
        /// <summary>
        /// The mouse's location last time update was called
        /// </summary>
        public MouseLocation LastMouseLocation { get { return lastLocation; } }

        public GEInput(Game game) : base(game) { }

        public override void Initialize()
        {
            buttonList = new ButtonList();
            keyList = new KeyList();

            MouseState state = new MouseState();

            currentLocation.X = state.X;
            currentLocation.Y = state.Y;

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();

            lastLocation.X = currentLocation.X;
            lastLocation.Y = currentLocation.Y;

            buttonList.Clear();
            buttonList.Add(mouseState.LeftButton);
            buttonList.Add(mouseState.MiddleButton);
            buttonList.Add(mouseState.RightButton);

            keyList.Clear();

            foreach (Keys k in Keyboard.GetState().GetPressedKeys())
                keyList.Add(k);

            
            base.Update(gameTime);
        }


    }
}
